package fr.imag.ufrima.ima.project.ui.listerners;

import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputAdapter;

import fr.imag.ufrima.ima.project.ui.main.ImageDocument;
import fr.imag.ufrima.ima.project.ui.main.ImageProject;

/**
 * 
 * @author lancelog
 *
 */
public class StatusBarListener extends MouseInputAdapter {
	
	private int w, h;
	String c1, c2, r, g, b, y, u, v; 
	private ImageProject frame;
	private ImageDocument id;
	
	/**
	 * Constructor
	 * @param frame
	 */
	public StatusBarListener(ImageProject frame){
		this.frame = frame;
		id=null;
		c1 = "";
		c2 = "";
		r = "";
		g = "";
		b = "";
		y = "";
		u = "";
		v = "";
	}
	
	/**
	 * 
	 */
	public void mouseEntered(MouseEvent me){
		id = frame.getCurrentImageDocument();
		w= id.getImage().getWidth();
		h= id.getImage().getHeight();
	}
	
	/**
	 * 
	 */
	public void mouseMoved(MouseEvent me){
		int xsouris=me.getX();
		int ysouris=me.getY();

		w= id.getImage().getWidth();
		h= id.getImage().getHeight();
		
		if( xsouris>=0 && xsouris<w && ysouris>=0 && ysouris<h ){
			c1 = xsouris +"";
			c2 = ysouris +"";
			frame.getStatusBar().setCoordonnees(c1,c2);
			int red = id.getImage().getTab()[xsouris][ysouris][1];
			int green = id.getImage().getTab()[xsouris][ysouris][2];
			int blue = id.getImage().getTab()[xsouris][ysouris][3];
			r = red+"";
			g = green+"";
			b = blue+"";
			frame.getStatusBar().setRGB(r,g,b);
			y = (int)(red*0.299+green*0.587+blue*0.114)+"";
			u = (int)(red*(-0.14713)-green*0.28886+blue*0.436)+"";
			v = (int)(red*0.615-green*0.51498-blue*0.10001)+"";
			frame.getStatusBar().setYUV(y,u,v);	
		}else{
			frame.getStatusBar().setCoordonnees("","");
			frame.getStatusBar().setRGB("","","");
			frame.getStatusBar().setYUV("","","");
		}
	}

	public void mouseExited(MouseEvent me){
		id=null;
		frame.getStatusBar().setCoordonnees("","");
		frame.getStatusBar().setRGB("","","");
		frame.getStatusBar().setYUV("","","");
		w=0;
		h=0;
	}
}
